"""字典类型配置

包含dict类型的C++映射和方法配置，以及字典视图类型（dict_keys, dict_values, dict_items）

使用 mc::dict 实现，保持 Python 3.7+ 的插入顺序语义
视图对象实现零拷贝引用，符合 Python 官方语义
"""

# 字典类型配置
CONFIG = {
    "dict[K, V]": {
        "cpp_type": "mc::dict",
        "includes": ["<mc/dict.h>"],
        "default_value": "mc::dict()",
        "pass_by": "const_ref",  # 容器按常量引用传递
        "generic_params": ["K", "V"],  # 泛型参数
        "iterator_element_type": "K",  # 迭代器元素类型（dict 迭代键）
        "satisfies_protocols": [
            "Mapping",
            "Iterable",
            "Sized",
            "Container",
            "Reversible",
        ],  # 满足的 Protocol
        "methods": {
            # ===== 基础访问方法 =====
            "get": {
                "signatures": [
                    "get(self, key: K) -> V | None",
                    "get(self, key: K, default: V) -> V",
                ],
                "templates": [
                    "mcpy::dict_get<{V}>({self}, {key})",
                    "mcpy::dict_get<{V}>({self}, {key}, {default})",
                ],
                "includes": ["mcpy/builtins/dict.h"],
                "priority": 1,
            },
            "keys": {
                "signatures": ["keys(self) -> dict_keys[K]"],
                "templates": ["mcpy::dict_keys<{K}>({self})"],
                "includes": ["mcpy/builtins/dict.h"],
                "priority": 1,
            },
            "values": {
                "signatures": ["values(self) -> dict_values[V]"],
                "templates": ["mcpy::dict_values<{V}>({self})"],
                "includes": ["mcpy/builtins/dict.h"],
                "priority": 1,
            },
            "items": {
                "signatures": ["items(self) -> dict_items[K, V]"],
                "templates": ["mcpy::dict_items<{K}, {V}>({self})"],
                "includes": ["mcpy/builtins/dict.h"],
                "priority": 1,
            },
            # ===== 修改方法 =====
            "clear": {
                "signatures": ["clear(self) -> None"],
                "templates": ["{self}.clear()"],
                "includes": [],
                "priority": 1,
                "mutating": True,
            },
            "pop": {
                "signatures": [
                    "pop(self, key: K) -> V",
                    "pop(self, key: K, default: V) -> V",
                ],
                "templates": [
                    "mcpy::dict_pop<{V}>({self}, {key})",
                    "mcpy::dict_pop<{V}>({self}, {key}, {default})",
                ],
                "includes": ["mcpy/builtins/dict.h"],
                "priority": 1,
                "mutating": True,
            },
            "popitem": {
                "signatures": ["popitem(self) -> tuple[K, V]"],
                "templates": ["mcpy::dict_popitem<{K}, {V}>({self})"],
                "includes": ["mcpy/builtins/dict.h"],
                "priority": 1,
                "mutating": True,
            },
            "update": {
                "signatures": ["update(self, other: dict[K, V]) -> None"],
                "templates": ["mcpy::dict_update({self}, {other})"],
                "includes": ["mcpy/builtins/dict.h"],
                "priority": 1,
                "mutating": True,
            },
            "setdefault": {
                "signatures": [
                    "setdefault(self, key: K) -> V",
                    "setdefault(self, key: K, default: V) -> V",
                ],
                "templates": [
                    "mcpy::dict_setdefault({self}, {key})",
                    "mcpy::dict_setdefault({self}, {key}, {default})",
                ],
                "includes": ["mcpy/builtins/dict.h"],
                "priority": 1,
                "mutating": True,
            },
            "copy": {
                "signatures": ["copy(self) -> dict[K, V]"],
                "templates": ["{self}.copy()"],
                "includes": [],
                "priority": 1,
            },
            "__len__": {
                "signatures": ["__len__(self) -> int"],
                "templates": ["{self}.size()"],
                "includes": [],
                "priority": 1,
            },
            "__iter__": {
                "signatures": ["__iter__(self) -> Iterator[K]"],
                "templates": ["mcpy::iter_wrapper({self})"],
                "includes": ["mcpy/builtins/global_functions.h"],
                "priority": 1,
            },
            "__bool__": {
                "signatures": ["__bool__(self) -> bool"],
                "templates": ["!{self}.empty()"],
                "includes": [],
                "priority": 1,
            },
            "__contains__": {
                "signatures": ["__contains__(self, item: K) -> bool"],
                "templates": ["{self}.contains({item})"],
                "includes": [],
                "priority": 1,
            },
            "__getitem__": {
                "signatures": ["__getitem__(self, index: K) -> V"],
                "templates": ["{self}[{index}]"],
                "includes": [],
                "priority": 1,
            },
            "__setitem__": {
                "signatures": ["__setitem__(self, index: K, value: V) -> None"],
                "templates": ["{self}[{index}] = {value}"],
                "includes": [],
                "priority": 1,
                "mutating": True,
            },
            "__delitem__": {
                "signatures": ["__delitem__(self, index: K) -> None"],
                "templates": ["mcpy::dict_delitem({self}, {index})"],
                "includes": ["mcpy/builtins/dict.h"],
                "priority": 1,
                "mutating": True,
            },
            "__eq__": {
                "signatures": ["__eq__(self, other: dict[K, V]) -> bool"],
                "templates": ["{self} == {other}"],
                "includes": [],
                "priority": 1,
            },
            "__ne__": {
                "signatures": ["__ne__(self, other: dict[K, V]) -> bool"],
                "templates": ["{self} != {other}"],
                "includes": [],
                "priority": 1,
            },
            "__or__": {
                "signatures": ["__or__(self, other: dict[K, V]) -> dict[K, V]"],
                "templates": ["mcpy::dict_merge({self}, {other})"],
                "includes": ["mcpy/builtins/dict.h"],
                "priority": 1,
            },
            "__ior__": {
                "signatures": ["__ior__(self, other: dict[K, V]) -> dict[K, V]"],
                "templates": ["mcpy::dict_ior({self}, {other})"],
                "includes": ["mcpy/builtins/dict.h"],
                "priority": 1,
                "mutating": True,
            },
            "__repr__": {
                "signatures": ["__repr__(self) -> str"],
                "templates": ["mcpy::dict_repr({self})"],
                "includes": ["mcpy/builtins/dict.h"],
                "priority": 1,
            },
            "__str__": {
                "signatures": ["__str__(self) -> str"],
                "templates": ["mcpy::dict_repr({self})"],
                "includes": ["mcpy/builtins/dict.h"],
                "priority": 1,
            },
            "fromkeys": {
                "signatures": [
                    "fromkeys(keys: list[K]) -> dict[K, None]",
                    "fromkeys(keys: list[K], value: V) -> dict[K, V]",
                ],
                "templates": [
                    "mcpy::dict_fromkeys({keys})",
                    "mcpy::dict_fromkeys({keys}, {value})",
                ],
                "includes": ["mcpy/builtins/dict.h"],
                "priority": 1,
                "is_classmethod": True,  # 标记为类方法
            },
            "__reversed__": {
                "signatures": ["__reversed__(self) -> Iterator[K]"],
                "templates": ["mcpy::dict_reversed<{K}>({self})"],
                "includes": ["mcpy/builtins/dict.h"],
                "priority": 1,
            },
        },
        # 构造函数配置
        "constructors": {
            "signatures": [
                "dict() -> dict[K, V]",
                "dict(items: list[tuple[K, V]]) -> dict[K, V]",
                "dict(other: dict[K, V]) -> dict[K, V]",
            ],
            "templates": [
                "mc::dict()",
                "mcpy::dict_construct({items})",
                "mc::dict({other})",
            ],
            "includes": ["<mc/dict.h>", "mcpy/builtins/dict.h"],
            "priority": 1,
        },
    },
    # 无类型注解的字典配置（弱类型）
    "dict": {
        "cpp_type": "mc::dict",
        "default_constructor_syntax": "parens",  # 使用 () 构造
        "includes": ["<mc/dict.h>", "mcpy/builtins/dict.h"],
        "default_value": "mc::dict()",
        "is_type": True,
        "module": "builtins",
        # 继承dict[K, V]的所有方法
        "inherit_from": "dict[K, V]",
        "type_substitution": {"K": "Any", "V": "Any"},
        "satisfies_protocols": [
            "Mapping",
            "Iterable",
            "Sized",
            "Container",
            "Reversible",
        ],  # 满足的 Protocol
    },
    # Python 3.9+ 兼容性：大写类型映射
    "Dict": {
        "is_type": True,
        "type_alias": "dict",
        "module": "typing",
    },
    # ===== 字典视图类型 =====
    "dict_keys[K]": {
        "cpp_type": "mcpy::dict_keys_view<K>",
        "includes": ["mcpy/builtins/dict_views.h"],
        "is_type": True,
        "module": "builtins",
        "generic_params": ["K"],  # 泛型参数
        "iterator_element_type": "K",  # 迭代器元素类型
        "satisfies_protocols": ["Iterable", "Sized", "Container"],  # 满足的 Protocol
        "methods": {
            "__len__": {
                "signatures": ["__len__(self) -> int"],
                "templates": ["{self}.size()"],
                "includes": [],
                "priority": 1,
            },
            "__contains__": {
                "signatures": ["__contains__(self, item: K) -> bool"],
                "templates": [
                    "std::find({self}.begin(), {self}.end(), {item}) != {self}.end()"
                ],
                "includes": ["<algorithm>"],
                "priority": 1,
            },
            "__iter__": {
                "signatures": ["__iter__(self) -> Iterator[K]"],
                "templates": ["mcpy::iter_wrapper({self})"],
                "includes": ["mcpy/builtins/global_functions.h"],
                "priority": 1,
            },
            "__next__": {
                "signatures": ["__next__(self) -> K"],
                "templates": ["{self}.__next__<{K}>()"],
                "includes": ["mcpy/builtins/global_functions.h"],
                "priority": 1,
            },
            "__getitem__": {
                "signatures": ["__getitem__(self, index: int) -> K"],
                "templates": ["{self}[{index}]"],
                "includes": [],
                "priority": 1,
            },
        },
    },
    "dict_values[V]": {
        "cpp_type": "mcpy::dict_values_view<V>",
        "includes": ["mcpy/builtins/dict_views.h"],
        "is_type": True,
        "module": "builtins",
        "generic_params": ["V"],  # 泛型参数
        "iterator_element_type": "V",  # 迭代器元素类型
        "satisfies_protocols": ["Iterable", "Sized"],  # 满足的 Protocol
        "methods": {
            "__len__": {
                "signatures": ["__len__(self) -> int"],
                "templates": ["{self}.size()"],
                "includes": [],
                "priority": 1,
            },
            "__iter__": {
                "signatures": ["__iter__(self) -> Iterator[V]"],
                "templates": ["{self}"],
                "includes": [],
                "priority": 1,
            },
            "__getitem__": {
                "signatures": ["__getitem__(self, index: int) -> V"],
                "templates": ["{self}[{index}]"],
                "includes": [],
                "priority": 1,
            },
        },
    },
    "dict_items[K, V]": {
        "cpp_type": "mcpy::dict_items_view<K, V>",
        "includes": ["mcpy/builtins/dict_views.h"],
        "is_type": True,
        "module": "builtins",
        "generic_params": ["K", "V"],  # 泛型参数
        "iterator_element_type": "tuple[K, V]",  # 迭代器元素类型（满足 Iterable[T] 和 Iterator[T] 中的 T）
        "satisfies_protocols": ["Iterable", "Sized", "Container"],  # 满足的 Protocol
        "methods": {
            "__len__": {
                "signatures": ["__len__(self) -> int"],
                "templates": ["{self}.size()"],
                "includes": [],
                "priority": 1,
            },
            "__iter__": {
                "signatures": ["__iter__(self) -> Iterator[tuple[K, V]]"],
                "templates": ["mcpy::iter_wrapper({self})"],
                "includes": ["mcpy/builtins/global_functions.h"],
                "priority": 1,
            },
            "__next__": {
                "signatures": ["__next__(self) -> tuple[K, V]"],
                "templates": ["{self}.__next__<std::tuple<{K}, {V}>>()"],
                "includes": ["mcpy/builtins/global_functions.h"],
                "priority": 1,
            },
            "__getitem__": {
                "signatures": ["__getitem__(self, index: int) -> tuple[K, V]"],
                "templates": ["{self}[{index}]"],
                "includes": [],
                "priority": 1,
            },
        },
    },
}
